var __UIPlayer_instance = null;
function getUIPlayer() { return __UIPlayer_instance; }

var UIPlayer = function(){};

UIPlayer.prototype.onDidLoadFromCCB = function()
{
    cc.log("UIPlayer.onDidLoadFromCCB");
    __UIPlayer_instance = this;
    
    
    this._cards = {}; // 1~6
    var poss = [cc.p(12,172), cc.p(12,92), cc.p(12,12), cc.p(74,172), cc.p(74,92), cc.p(74,12)];
    

    // 创建CardImage类
    for(var i in poss) {
        i = parseInt(i); //* 不知道为何i是string
        p = poss[i];
        card = new CardImage('', '', 0, 0);
        card.setPosition(p);
        card.setScale(0.4); // 缩放大小
        card.showImage(false);
        this.uiCardArray.addChild(card);
        this._cards[i+1] = card;
    }
    
    // 当前选中
    this._curPos = null;
    this._curCardId = null;
    this._curCardImage = new CardImage('', '', 0, 0);
    this._curCardImage.setPosition(cc.p(24,68));
    this._curCardImage.setScale(0.9); // 缩放大小
    this._curCardImage.showImage(false);
    this.uiCardInfo.addChild(this._curCardImage);
    
    // 默认设置为空
    this.setCurrent(null);
};

UIPlayer.prototype.updateData = function()
{
    cc.log('UIPlayer.updateData');
    
    var ba = DataManager.playerBattleArray;

    for (var _pos in this._cards) {
        var pos = parseInt(_pos);
        card = this._cards[pos];
        cardId = ba[pos];

        if (cardId) {
            setCardImage(card, cardId);
            card.showImage(true);
            card.setOnClickFunc( function (params) {
                                    getUIPlayer().setCurrent(params);
                                }, [pos, cardId] ); // 参数 pos, cardId
        } else {
            card.showImage(false);
            card.setOnClickFunc( function (params) {
                                    getUIPlayer().setCurrent(params);
                                }, [pos, null] ); // 参数 pos, cardId
        }
    }

    if(this._curCardId) {
        this.setCurrent([this._curPos, this._curCardId]);
    } else {
        this.setCurrent(null);
    }
}

// 卡牌点击响应
UIPlayer.prototype.onClickCard = function(params) {
    getUIPlayer().setCurrent(params);
}

// 卡牌详细信息界面
UIPlayer.prototype.setCurrent = function(params)
{
    cc.log('UIPlayer.setCurrent ' + params);

    if(params) {
        pos = params[0];
        cardId = params[1];
        this._curPos = pos;
        this._curCardId = cardId;
        
        setCardImage(this._curCardImage, cardId);
        this._curCardImage.showImage(true);
        
        var card = DataManager.playerCardData[cardId];
        if(! card) {
            this.clearCurrent();
            cc.log('playerCardData没有这个id:'+cardId);
            return;
        }
        
        //* 临时
        var d = DATA_pet[card.type];
        this.uiXue.setString(d.hp);
        this.uiGong.setString(d.attack);
        this.uiSu.setString(d.speed);
        this.uiFang.setString(d.defense);
        this.uiQianneng.setString('潜能 '+'999');
        this.uiWeijie.setString('位阶 '+'一介');
    }
    else {
        this.clearCurrent();
    }
}

UIPlayer.prototype.clearCurrent = function(params)
{
    this._curCardImage.showImage(false);
    
    this.uiXue.setString(' ');
    this.uiGong.setString(' ');
    this.uiSu.setString(' ');
    this.uiFang.setString(' ');
    this.uiQianneng.setString('潜能');
    this.uiWeijie.setString('位阶');
}

// 更换卡牌
UIPlayer.prototype.onChangeCard = function(target)
{
    cc.log('onChangeCard');
    if(!this._curPos) return;

    getUIMain().showUIBag();
    getUIBag().setOnSelectFunc(function (cardId) {
        _this = getUIPlayer();
        _this.sendChangeCard(_this._curPos, cardId);
    });
}

UIPlayer.prototype.sendChangeCard = function(pos, cardId)
{
    gnet.sendHttp('ba_change', [pos, cardId], function (data) {
        if(data['result']=='ok') {

            gnet.sendHttp('ba_get', [], function (_data) {
                if(data['result']=='ok') {
                    // 刷新数据
                    DataManager.setData(_data['data']);
                    // 界面更换
                    getUIMain().showUIPlayer(); // 返回UIPlayer页面
                    getUIPlayer().updateData();
                }
            });

        }
    });
}

UIPlayer.prototype.onLevelUp = function(target) 
{
    if(! this._curCardId) return;

    getUIMain().showUICardLevelUp(true);
    getUICardLevelUp().setCurrent(this._curCardId);
}

UIPlayer.prototype.onQualityUp = function(target) 
{
    if(! this._curCardId) return;

    getUIMain().showUICardQualityUp(true);
    getUICardQualityUp().setCurrent(this._curCardId);
}
